package com.neu.carbon.wms.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.neu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.neu.carbon.wms.domain.WmsOutWarehouseApplyDetail;
import com.neu.carbon.wms.mapper.WmsOutWarehouseApplyMapper;
import com.neu.carbon.wms.domain.WmsOutWarehouseApply;
import com.neu.carbon.wms.service.IWmsOutWarehouseApplyService;

/**
 * 出库申请Service业务层处理
 * 
 * @author neuedu
 * @date 2024-10-09
 */
@Service
public class WmsOutWarehouseApplyServiceImpl implements IWmsOutWarehouseApplyService 
{
    @Autowired
    private WmsOutWarehouseApplyMapper wmsOutWarehouseApplyMapper;

    /**
     * 查询出库申请
     * 
     * @param id 出库申请ID
     * @return 出库申请
     */
    @Override
    public WmsOutWarehouseApply selectWmsOutWarehouseApplyById(Long id)
    {
        return wmsOutWarehouseApplyMapper.selectWmsOutWarehouseApplyById(id);
    }

    /**
     * 查询出库申请列表
     * 
     * @param wmsOutWarehouseApply 出库申请
     * @return 出库申请
     */
    @Override
    public List<WmsOutWarehouseApply> selectWmsOutWarehouseApplyList(WmsOutWarehouseApply wmsOutWarehouseApply)
    {
        return wmsOutWarehouseApplyMapper.selectWmsOutWarehouseApplyList(wmsOutWarehouseApply);
    }

    /**
     * 新增出库申请
     * 
     * @param wmsOutWarehouseApply 出库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsOutWarehouseApply(WmsOutWarehouseApply wmsOutWarehouseApply)
    {
        int rows = wmsOutWarehouseApplyMapper.insertWmsOutWarehouseApply(wmsOutWarehouseApply);
        insertWmsOutWarehouseApplyDetail(wmsOutWarehouseApply);
        return rows;
    }

    /**
     * 修改出库申请
     * 
     * @param wmsOutWarehouseApply 出库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsOutWarehouseApply(WmsOutWarehouseApply wmsOutWarehouseApply)
    {
        wmsOutWarehouseApplyMapper.deleteWmsOutWarehouseApplyDetailByApplyId(wmsOutWarehouseApply.getId());
        insertWmsOutWarehouseApplyDetail(wmsOutWarehouseApply);
        return wmsOutWarehouseApplyMapper.updateWmsOutWarehouseApply(wmsOutWarehouseApply);
    }

    /**
     * 批量删除出库申请
     * 
     * @param ids 需要删除的出库申请ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsOutWarehouseApplyByIds(Long[] ids)
    {
        wmsOutWarehouseApplyMapper.deleteWmsOutWarehouseApplyDetailByApplyIds(ids);
        return wmsOutWarehouseApplyMapper.deleteWmsOutWarehouseApplyByIds(ids);
    }

    /**
     * 删除出库申请信息
     * 
     * @param id 出库申请ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsOutWarehouseApplyById(Long id)
    {
        wmsOutWarehouseApplyMapper.deleteWmsOutWarehouseApplyDetailByApplyId(id);
        return wmsOutWarehouseApplyMapper.deleteWmsOutWarehouseApplyById(id);
    }

    /**
     * 新增出库申请明细信息
     * 
     * @param wmsOutWarehouseApply 出库申请对象
     */
    public void insertWmsOutWarehouseApplyDetail(WmsOutWarehouseApply wmsOutWarehouseApply)
    {
        List<WmsOutWarehouseApplyDetail> wmsOutWarehouseApplyDetailList = wmsOutWarehouseApply.getWmsOutWarehouseApplyDetailList();
        Long id = wmsOutWarehouseApply.getId();
        if (StringUtils.isNotNull(wmsOutWarehouseApplyDetailList))
        {
            List<WmsOutWarehouseApplyDetail> list = new ArrayList<WmsOutWarehouseApplyDetail>();
            for (WmsOutWarehouseApplyDetail wmsOutWarehouseApplyDetail : wmsOutWarehouseApplyDetailList)
            {
                wmsOutWarehouseApplyDetail.setApplyId(id);
                list.add(wmsOutWarehouseApplyDetail);
            }
            if (list.size() > 0)
            {
                wmsOutWarehouseApplyMapper.batchWmsOutWarehouseApplyDetail(list);
            }
        }
    }
}
